package com.wxsoft.business.sys.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxsoft.business.common.controller.BaseController;
import com.wxsoft.business.model.easyui.DataGrid;
import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.sys.pojo.SysUser;
import com.wxsoft.business.sys.service.ISysUserService;
import com.wxsoft.util.UserUtil;

@Controller
@RequestMapping("/sysUser")
public class SysUserController extends BaseController{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private ISysUserService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "sys/sysUserlist";
	}

    @ResponseBody
	@RequestMapping("/datagrid")
	public DataGrid list(HttpServletRequest request,PageHelper page, SysUser sysUser,HttpServletResponse response) throws IOException{
		
		DataGrid dg = new DataGrid();
		dg.setTotal(service.findCount(sysUser));
		List<SysUser> sysUserList = service.findAll(page, sysUser);
		dg.setRows(sysUserList);
//		JsonUtil.toResponse(response, dg);
		return dg ;
	}
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST )
	public void add(HttpServletRequest request,HttpServletResponse response,
			@Valid SysUser sysUser, BindingResult bindingResult) {
		try {
//			String start_date_str =request.getParameter("start_date_str");
//			sysUser.setStart_date(DateUtil.getDate(start_date_str,"yyyy-MM-dd"));
//			String end_date_str =request.getParameter("end_date_str");
//			sysUser.setEnd_date(DateUtil.getDate(end_date_str,"yyyy-MM-dd"));
			if (bindingResult.hasErrors()) {
				this.writeReturn(request,response, bindingResult);
				return ;
			}
			
			
			SysUser user = UserUtil.getUser(request);
			sysUser.setHandle(user.getUser_id());
			sysUser.setHandle_date(new Date());
			String r = service.add(sysUser);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modify(HttpServletRequest request,HttpServletResponse response, SysUser sysUser) {
		try {
//			String start_date_str =request.getParameter("start_date_str");
//			sysUser.setStart_date(DateUtil.getDate(start_date_str,"yyyy-MM-dd"));
//			String end_date_str =request.getParameter("end_date_str");
//			sysUser.setEnd_date(DateUtil.getDate(end_date_str,"yyyy-MM-dd"));
			
			SysUser user = UserUtil.getUser(request);
			sysUser.setHandle(user.getUser_id());
			sysUser.setHandle_date(new Date());
			String r = service.modify(sysUser);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	
//	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request,HttpServletResponse response, SysUser sysUser) {
		try {
			String r = service.delete(sysUser);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
}
